package com.jsyh.maidu.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jsyh.maidu.R;
import com.jsyh.maidu.adapter.goods.GridAdapter;
import com.jsyh.maidu.adapter.home.BrandListMoreAdapter;
import com.jsyh.maidu.adapter.home.GoodsListMoreAdapter;
import com.jsyh.maidu.adapter.home.RefreshRecyclerView;
import com.jsyh.maidu.model.BestNewHot;
import com.jsyh.maidu.model.BestNewHotModel;
import com.jsyh.maidu.model.Brands;
import com.jsyh.maidu.model.BrandsModel;
import com.jsyh.maidu.model.FilterInfoModel;
import com.jsyh.maidu.model.GoodsInfo;
import com.jsyh.maidu.model.GoodsInfoModel;
import com.jsyh.maidu.presenter.GoodsFilterPresenter;
import com.jsyh.maidu.presenter.GoodsListMorePresenter;
import com.jsyh.maidu.views.BestNewHotView;
import com.jsyh.maidu.views.BrandsView;
import com.jsyh.maidu.views.GoodsFilterView;
import com.jsyh.shopping.uilibrary.GoodsFilterTabView;

import java.util.ArrayList;
import java.util.List;


public class GoodsListActivity extends BaseActivity implements
        BestNewHotView, BrandsView, View.OnClickListener,
        RefreshRecyclerView.OnLoadMoreListener, GoodsFilterTabView.FilterTabListener,
        GoodsFilterView {

    private RefreshRecyclerView goodsList;
    private GoodsListMoreAdapter adapter;
    private GridLayoutManager gridLayoutManager;
    private GoodsListMorePresenter presenter;
    private SwipeRefreshLayout refresh;
    private String type;
    private String url;
    private List<BestNewHot> datas;
    private List<Brands> brandData;
    private TextView title;
    private FrameLayout back;
    private ImageView img;
    private boolean datachtype = false;
    private GoodsFilterTabView filterView;
    private String mainType = "new";
    private GoodsFilterPresenter goodsFilterPresenter;
    private String order;

    private GridAdapter mGridAdapter;           //商品展示适配

    private List<GoodsInfo> mGoodsDatas;
    private int page = 1;


    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_goods_list);
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        //disId = intent.getStringExtra("disid");
        goodsList = (RefreshRecyclerView) findViewById(R.id.goods_list);
        title = (TextView) findViewById(R.id.title);
        back = (FrameLayout) findViewById(R.id.fl_Left);
        refresh = (SwipeRefreshLayout) findViewById(R.id.goods_list_refresh);
        img = (ImageView) findViewById(R.id.goods_list_img);
        filterView = (GoodsFilterTabView) findViewById(R.id.goods_list_filter);
        filterView.setFilterGone();
        filterView.setOnTabListener(this);
        back.setOnClickListener(this);
        goodsList.setLoadMoreEnable(true);
        goodsList.setOnLoadMoreListener(this);
        gridLayoutManager = new GridLayoutManager(this, 2);
        if (type.equals("brands")) {
            title.setText("品牌街");
            img.setVisibility(View.VISIBLE);
            filterView.setVisibility(View.GONE);
        } else if (type.equals("new1")) {
            title.setText("新品上市");
            img.setVisibility(View.GONE);
            filterView.setVisibility(View.VISIBLE);
        } else if (type.equals("hot1")) {
            title.setText("热销商品");
            img.setVisibility(View.GONE);
            filterView.setVisibility(View.VISIBLE);
        } else if (type.equals("promotion")) {
            title.setText("活动商品");
            img.setVisibility(View.GONE);
            filterView.setVisibility(View.VISIBLE);
        }
        goodsList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int topRowVerticalPosition =
                        (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                refresh.setEnabled(topRowVerticalPosition >= 0);

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        presenter = new GoodsListMorePresenter(this, this);
        goodsFilterPresenter = new GoodsFilterPresenter(this);
        if (!TextUtils.isEmpty(type)) {
            if (type.equals("brands")) {
                url = typeUrl(type);
                presenter.getMoreList(this, type, url);
            } else {
                order = filterView.getOrders();
                goodsFilterPresenter.loadIndexMore(this, page + "", order, mainType, type);
            }

        }
        mGoodsDatas = new ArrayList<>();
        mGridAdapter = new GridAdapter(this, R.layout.goods_show_model_two_item, mGoodsDatas);
//        goodsList.setLayoutManager(gridLayoutManager);
//        goodsList.setAdapter(mGridAdapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                datachtype = false;
                page = 1;
                if (type.equals("brands")) {
                    presenter.setPage(page);
                    presenter.getMoreList(GoodsListActivity.this, type, url);
                } else {
                    order = filterView.getOrders();
                    goodsFilterPresenter.loadIndexMore(GoodsListActivity.this, page + "", order, mainType, type);
                }
            }
        });

    }


    private String typeUrl(String type) {
        String url;
        if (type.equals("home_goods")) {
            url = "first/home_goods";
        } else if (type.equals("brands")) {
            url = "first/brands";
        } else {
            url = "first/goods_more";
        }
        return url;
    }

    @Override
    public void bestNewHotData(BestNewHotModel model) {
        if (model.getCode().equals("200")) {
            refresh.setRefreshing(false);
            datas = model.getData();

            if (datachtype) {
                adapter.addAll(datas);
                goodsList.switchLayoutManager(gridLayoutManager);
                goodsList.notifyData();
            } else {
                adapter = new GoodsListMoreAdapter(GoodsListActivity.this, datas, type);
                gridLayoutManager = new GridLayoutManager(GoodsListActivity.this, 2);
                goodsList.setLayoutManager(gridLayoutManager);


            }
            goodsList.setAdapter(adapter);
            goodsList.setItemAnimator(new DefaultItemAnimator());
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_Left:
                finish();
                break;
        }
    }


    @Override
    public void loadMoreListener() {
        datachtype = true;

        page++;
        if (type.equals("brands")) {
            presenter.setPage(page);
            presenter.getMoreList(GoodsListActivity.this, type, url);
        } else {
            order = filterView.getOrders();
            goodsFilterPresenter.loadIndexMore(GoodsListActivity.this, page + "", order, mainType, type);
        }
    }

    @Override
    public void getBrandsMore(BrandsModel brands) {
        if (brands.getCode().equals("1")) {
            refresh.setRefreshing(false);
            brandData = brands.getData();
            BrandListMoreAdapter adapter = new BrandListMoreAdapter(GoodsListActivity.this, brandData);
            gridLayoutManager = new GridLayoutManager(GoodsListActivity.this, 3);
            goodsList.setLayoutManager(gridLayoutManager);
            goodsList.setAdapter(adapter);
            goodsList.setItemAnimator(new DefaultItemAnimator());
            //goodsList.addItemDecoration(new DividerGridItemDecoration(GoodsListActivity.this), DividerItemDecoration.HORIZONTAL_LIST);
        }
    }

    @Override
    public void onFilterListener() {

    }

    @Override
    public void onFilterCondition(String condition) {
        if (condition.equals("2") | condition.equals("1")) {
            mainType = "price";
        } else if (condition.equals("3") | condition.equals("6")) {
            mainType = "hot";
        } else if (condition.equals("0") | condition.equals("5")) {
            mainType = "new";
        } else if (condition.equals("4") | condition.equals("7")) {
            mainType = "best";
        }
        order = filterView.getOrders();
        goodsFilterPresenter.loadIndexMore(this, page + "", order, mainType, type);
    }

    @Override
    public void onLayoutSwitch(int model, int firstPosition) {

    }


    @Override
    public void onFilterGoodsData(@Nullable GoodsInfoModel model, @Nullable Exception e) {
        refresh.setRefreshing(false);
        if (model == null) {
            return;
        }

        if (model.getData().getGoods() == null || model.getData().getGoods().isEmpty()) {
            if (page <= 1) {
                //第一次，没有加载到数据
                mGoodsDatas.clear();
                return;
            } else {
                Toast.makeText(GoodsListActivity.this, R.string.no_more, Toast.LENGTH_SHORT).show();
                return;
            }
        }

//        try {
//            if (!TextUtils.isEmpty(model.getData().getPage_count())) {
//                mPageCount = Integer.parseInt(model.getData().getPage_count());
//                if (page <= mPageCount) {
//                    page++;
//                }
//            }
//        } catch (NumberFormatException e1) {
//            e1.printStackTrace();
//        }
        if (page <= 1) {
            mGoodsDatas.clear();
            goodsList.setAdapter(mGridAdapter);
        }

        mGoodsDatas.addAll(model.getData().getGoods());
        goodsList.setLayoutManager(gridLayoutManager);
        goodsList.notifyData();

        //mGridAdapter.notifyDataSetChanged();

    }

    @Override
    public void onDrawerData(@Nullable FilterInfoModel model, @Nullable Exception e) {

    }
}
